﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors;
using MINERP.BLL.InfoConfig;
using MINERP.DAL;
using MINERP.MODEL.InfoConfig;

namespace MINERP.InfoConfig.Controller
{
    public  class PaperTemplateController
    {
        #region //private fields
        List<PaperTemplateEntity> _paperTemplateEntityList = null;
        private PaperTemplateBLL _bll = null;
        private GridControl _grid = null;
        private GridView _view = null;
        private ComboBoxEdit _comboBox = null;
        private GroupControl _groupControl = null;
        private CheckEdit _checkEdit = null;

        #endregion


        #region //Public Properties

        internal GroupControl GroupControl
        {
            set
            {
                _groupControl = value;
            }
        }
        internal CheckEdit CheckEdit
        {
            set
            {
                _checkEdit = value;
            }
        }
        internal ComboBoxEdit ComoBox
        {
            set
            {
                _comboBox = value;
            }
        }
        internal GridControl Grid
        {
            set
            {
                _grid = value;
            }
        }

        internal GridView View
        {
            set
            {
                this._view = value;
            }
            get
            {
                return _view;
            }
        }

        #endregion

        #region //Public Methods

        /// <summary>
        /// 获得默认数据
        /// </summary>
        public void GetDate()
        {

            _bll = new PaperTemplateBLL();
            _paperTemplateEntityList = new List<PaperTemplateEntity>();
            foreach (var item in _bll.GetPaperTemplate())
	        {
                PaperTemplateEntity paperTemplateEntity = new PaperTemplateEntity();
		        paperTemplateEntity.TempLateId = item.TempLateId;
                paperTemplateEntity.TemplateName = item.TemplateName;
                paperTemplateEntity.BoardHeight = item.boardHeight;
                paperTemplateEntity.BoardLength = item.boardLength;
                paperTemplateEntity.PaperFaceName =item.paperFace.Name;
                paperTemplateEntity.PaperTypeName = item.paperType.Name;
                paperTemplateEntity.PName = item.printer.PName;
                _paperTemplateEntityList.Add(paperTemplateEntity);
	        }

            _grid.DataSource = _paperTemplateEntityList;
        }

        /// <summary>
        /// 添加纸模版
        /// </summary>
        /// <param name="obj"></param>
        public void Add(tb_paperTemplate obj)
        {
            _bll = new PaperTemplateBLL();
            _bll.Add(obj);
        }

        /// <summary>
        ///更新纸模版
        /// </summary>
        /// <param name="obj"></param>
        public void Update(PaperTemplateEntity obj)
        {
            PaperFaceBLL facebll = new PaperFaceBLL();
            PaperTypeBLL typebll = new PaperTypeBLL();
            PrinterBLL printerbll = new PrinterBLL();
            paperFace paperFace = facebll.GetPaperFaceObj(obj.PaperFaceName);
            paperType paperType = typebll.GetPaperTypeObj(obj.PaperTypeName);
            printer printer = printerbll.GetPrinterObj(obj.PName);
            _bll = new PaperTemplateBLL();
            tb_paperTemplate paperTemplate = _bll.GetPaperTemplateObj(obj.TempLateId);
            paperTemplate.boardHeight = obj.BoardHeight;
            paperTemplate.boardLength = obj.BoardLength;
            paperTemplate.TemplateName = obj.TemplateName;
            paperTemplate.paperType = paperType;
            paperTemplate.paperFace = paperFace;
            paperTemplate.printer = printer;

            _bll.Update();
        }
        /// <summary>
        /// 删除纸模版
        /// </summary>
        /// <param name="obj"></param>
        public void Delete(PaperTemplateEntity obj)
        {
            _bll = new PaperTemplateBLL();
            tb_paperTemplate paperTemplate = _bll.GetPaperTemplateObj(obj.TempLateId);
            _bll.Delete(paperTemplate);
        }
        /// <summary>
        /// 得到纸模版对象
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public tb_paperTemplate GetPaperFaceObj(int id)
        {
            _bll = new PaperTemplateBLL();
            return _bll.GetPaperTemplateObj(id);
        }


        /// <summary>
        /// 绑定纸面
        /// </summary>
        public void LinkcmbPaperFace()
        {
            PaperFaceBLL bll = new PaperFaceBLL();
            List<paperFace> list = bll.GetPaperFaceList();
            foreach (paperFace obj in list)
            {
                _comboBox.Properties.Items.Add(obj.Name);
            }
        }
        /// <summary>
        /// 绑定纸型
        /// </summary>
        public void LinkcmbPaperType()
        {
            PaperTypeBLL bll = new PaperTypeBLL();
            List<paperType> list = bll.GetPaperTypeList();
            foreach (paperType obj in list)
            {
                _comboBox.Properties.Items.Add(obj.Name);
            }

        }

        public void LinkcmbPrinter()
        {
            List<printer> list = null;
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                list =entities.printer.ToList();
                foreach (var item in list)
                {
                    _comboBox.Properties.Items.Add(item.PName);
                }
            }
        }
        #endregion

       
    }
}
